package com.southminority.ethnic.controller.research;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.service.research.IntellectualPropertyService;
import com.southminority.ethnic.controller.research.vo.IntellectualPropertyVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识产权前端展示控制器
 * 
 * @author SouthMinority
 */
@RestController
@RequestMapping("/api/research/intellectual-property")
@Tag(name = "知识产权管理", description = "知识产权相关接口")
@CrossOrigin
public class IntellectualPropertyController {

    private final IntellectualPropertyService intellectualPropertyService;

    public IntellectualPropertyController(IntellectualPropertyService intellectualPropertyService) {
        this.intellectualPropertyService = intellectualPropertyService;
    }

    /**
     * 获取知识产权列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取知识产权列表")
    public R<List<IntellectualPropertyVO>> getIntellectualPropertyList() {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getIntellectualPropertyList();
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("获取知识产权列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取重点展示的知识产权列表
     */
    @GetMapping("/featured")
    @Operation(summary = "获取重点展示的知识产权列表")
    public R<List<IntellectualPropertyVO>> getFeaturedIntellectualPropertyList() {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getFeaturedIntellectualPropertyList();
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("获取重点知识产权列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取知识产权详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取知识产权详情")
    public R<IntellectualPropertyVO> getIntellectualPropertyDetail(
            @Parameter(description = "知识产权ID") @PathVariable Long id) {
        try {
            IntellectualPropertyVO intellectualPropertyVO = intellectualPropertyService.getIntellectualPropertyDetail(id);
            return R.ok(intellectualPropertyVO);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("获取知识产权详情失败: " + e.getMessage());
        }
    }

    /**
     * 根据类型获取知识产权列表
     */
    @GetMapping("/type/{type}")
    @Operation(summary = "根据类型获取知识产权列表")
    public R<List<IntellectualPropertyVO>> getIntellectualPropertyListByType(
            @Parameter(description = "知识产权类型") @PathVariable String type) {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getIntellectualPropertyListByType(type);
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("根据类型获取知识产权列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据年份获取知识产权列表
     */
    @GetMapping("/year/{year}")
    @Operation(summary = "根据年份获取知识产权列表")
    public R<List<IntellectualPropertyVO>> getIntellectualPropertyListByYear(
            @Parameter(description = "年份") @PathVariable Integer year) {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getIntellectualPropertyListByYear(year);
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("根据年份获取知识产权列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据法律状态获取知识产权列表
     */
    @GetMapping("/legal-status/{legalStatus}")
    @Operation(summary = "根据法律状态获取知识产权列表")
    public R<List<IntellectualPropertyVO>> getIntellectualPropertyListByLegalStatus(
            @Parameter(description = "法律状态") @PathVariable String legalStatus) {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getIntellectualPropertyListByLegalStatus(legalStatus);
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("根据法律状态获取知识产权列表失败: " + e.getMessage());
        }
    }

    /**
     * 前端分页查询：与论文分页风格一致，支持关键词/类型/法律状态筛选
     */
    @GetMapping("/page")
    @Operation(summary = "分页获取知识产权列表(前端)")
    public R<PageResult<IntellectualPropertyVO>> getIntellectualPropertyPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "6") @RequestParam(defaultValue = "6") Integer size,
            @Parameter(description = "关键词(标题/发明人/申请人)") @RequestParam(required = false) String keyword,
            @Parameter(description = "类型(patent/utility/software/dataset/trademark)") @RequestParam(required = false) String type,
            @Parameter(description = "法律状态(pending/granted/rejected/expired)") @RequestParam(required = false) String legalStatus
    ) {
        try {
            // 直接复用服务层分页方法，状态固定为1(启用)
            PageResult<IntellectualPropertyVO> result = intellectualPropertyService
                    .getIntellectualPropertyPage(page, size, keyword, type, legalStatus, 1);
            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("分页获取知识产权失败: " + e.getMessage());
        }
    }

    /**
     * 测试接口
     */
    @GetMapping("/test")
    @Operation(summary = "测试接口")
    public R<String> test() {
        return R.ok("知识产权服务正常");
    }
} 
